package cn.vawn.medical.service.impl;

import cn.vawn.base.dto.PageDTO;
import cn.vawn.base.dto.RequestPageDTO;
import cn.vawn.medical.entity.Organization;
import cn.vawn.medical.entity.Uniterm;
import cn.vawn.medical.mapper.OrganizationMapper;
import cn.vawn.medical.mapper.UnitermMapper;
import cn.vawn.medical.service.IOrganizationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("organizationService")
public class OrganizationServiceImpl implements IOrganizationService {

    @Resource
    private OrganizationMapper organizationMapper;

    @Resource
    private UnitermMapper unitermMapper;

    @Override
    public PageDTO searchUniterm(RequestPageDTO requestPageDTO, String name, Integer organizationId, Date startTime, Date endTime) {

        PageDTO pd = new PageDTO();

        int pageNumber = requestPageDTO.getPageNumber();
        int pageSize = requestPageDTO.getPageSize();
        String sort = requestPageDTO.getSort();
        String desc = requestPageDTO.getDesc();
        if(pageNumber > 0 &&  pageSize > 0){
            PageHelper.startPage(pageNumber, pageSize);
        }
        if(!sort.isEmpty() && !desc.isEmpty()){
            PageHelper.orderBy(sort + " " + desc);
        }

        List<Uniterm> searchList = unitermMapper.searchUniterm(name, organizationId, startTime, endTime);

        pd.setPageNumber(pageNumber);
        pd.setPageSize(pageSize);
        pd.setTotal((int) new PageInfo(searchList).getTotal());
        pd.setData(new PageInfo(searchList).getList());

        return pd;
    }

    @Override
    public List<Organization> getAllOrganization() {
        return organizationMapper.getAllOrganization();
    }

    @Override
    public void changeIsHomeStatistics(Uniterm uniterm) {
        unitermMapper.changeIsHomeStatistics(uniterm);
    }

    @Override
    public int getDefaultPaperIdByOrgId(int organizationId) {
        int defaultPaperId =  organizationMapper.getDefaulltPaperId(organizationId);
        return defaultPaperId;
    }


}
